function [signal,source] = chaos_generator(cfg)
% cfg: type, x0, numSamples, para, Npre, gains, snrDB
    
if strcmpi(cfg.type,'tent')
    f_map = @(para,x) para*(1-abs(2*x-1));
elseif strcmpi(cfg.type,'logistic')
    f_map = @(para,x) para*x * (1 - x);
else
    error('cfg.type');
end
if ~isfield(cfg,'Npre')
    cfg.Npre = 0;
end

if ~isfield(cfg,'gains')
    cfg.gains = 1;
end

if ~isfield(cfg,'snrDB')
    cfg.snrDB = 30;
end

% 预热过程
x = cfg.x0;
for i = 1:cfg.Npre
    x = f_map(cfg.para,x);
end

source = zeros(cfg.numSamples, 1);
% 生成混沌序列
source(1,1) = x;
for i = 2:cfg.numSamples
    x = f_map(cfg.para,x);
    source(i,1) = x;
end

source = source.*cfg.gains;

% 添加噪声
sigPow = var(source);
noisePow = sigPow/db2pow(cfg.snrDB);
noise = sqrt(noisePow)* randn(cfg.numSamples, 1);
signal = source + noise;
end
